﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

namespace TextListContentPipelineExtension
{
    /// <summary>
    /// The ContentImporter is the first step in the Content Pipeline. The Content Pipeline will instantiate an instance of
    /// this class for content which has this importer set as its importer. In our example here that's GameText.txt. Note that
    /// we needed to apply the ContentImporterAttribute, to specify the file extensions this processor would handle (that way when
    /// you add content to a content project, it can automatically apply your importers based on the file extension). We also specify
    /// the default processor that it should use (again so it can automatically apply that processor), and a display name (which is
    /// what you will see if you right click on GameText.txt in Solution Explorer and choose "Properties".
    /// </summary>
    [ContentImporter(".txt", DefaultProcessor="TextListContentProcessor", DisplayName="Text List Importer")]
    public class TextListContentImporter : ContentImporter<TextListContent>
    {
        /// <summary>
        /// This is the method that does the actual reading in of the file. There are few restrictions on what you can do
        /// in this method, but it's best just to read it in and (if necessary) do as little processing as possible in order to
        /// maintain a logical organization to your content pipeline extension.
        /// </summary>
        /// <param name="filename">The file we are currently importing.</param>
        /// <param name="context">The Content Pipeline-supplied ContentImporterContext.</param>
        /// <returns>Our unprocessed TextListContent read in from <paramref name="filename"/>.</returns>
        public override TextListContent Import(string filename, ContentImporterContext context)
        {
            //Create a new instance of TextListContent to read the data into.
            TextListContent textListContent = new TextListContent();

            // Under Tools->Options->Projects and Solutions->Build and Run, if you have "MSBuild project build output verbosity" set
            // to "Minimal" then you won't see LogMessage messages but only LogImportantMessage and LogWarning, the latter of which generates a
            // build warning that will appear in your Error List window in Visual Studio.
            context.Logger.LogMessage("Importing '{0}'.", filename);

            ////A sample LogWarning. Uncomment this and watch a warning with the description "Oh noes!" appear in your project warning.
            //context.Logger.LogWarning("Putting a url here, except to MSDN content, won't do you any good. :)", new ContentIdentity(filename), "Oh noes!");

            // Read in the text file using an ordinary StreamReader, adding each line to our TextListContent's TextData list.
            using (System.IO.StreamReader reader = new StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    textListContent.TextData.Add(reader.ReadLine());
                }
                reader.Close();
            }

            // Returns the TextListContent, which will then be automatically whisked away by the Content Pipeline
            // to TextListContentProcessor. Unless the user set some other processor for it, in which case it would go there. Though unless
            // that processor was expecting input of type TextListContent, it would fail with an error.
            return textListContent;
        }
    }
}
